<template>
  <view class="con-result-wraper">
    <view class="head-input">
      <view class="head-margin">修改密码</view>
      <view class="head-head" @click="toSettings">
        <view style="font-size: 15px">去设置</view>
        <uni-icons
          class="icons-margin"
          color="#8d8d8d"
          type="right"
          size="24"
        />
      </view>
    </view>
    <view class="onDevice" @click="onDevice(1)">{{onDeviceName}}</view>
    <view class="closeDevice" @click="onDevice(2)">{{closeDeviceName}}</view>
    <!-- <view class="car_wash_time">洗车券核心时长</view> -->
    <!-- <view class="minutes"><input :disabled="isOpen" v-model="minutes" type="number">分钟</view> -->
    <view class="operate-container">
      <view class="operate-button" @click="handleExit">退出登录 </view>
    </view>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
import { logout,sendMessageApi,getSwitchStatusApi} from "@/common/yftx-api.js";
import { getToken, removeToken } from "@/util/auth.js";
export default {
  data (){ 
    return { 
      minutes:0,
      onDeviceName:'开启设备',
      closeDeviceName:'关闭设备',
      isOpen:false
    }
  },
  onLoad(options) {
    this.getStatus()
  },
  methods: {
    // 点击去设置
    toSettings() {
      console.log("点击去设置");
      uni.navigateTo({
        url: "./changePassword",
      });
    },

    // 退出
    handleExit() {
      let token = getToken();
      logout(token).then((res) => {
        if (res && res.code === 200) {
          removeToken();
          this.$refs.uToast.show({
            message: res.msg,
            duration: 2000,
          });
          uni.setStorageSync("logout", "today");
          uni.redirectTo({
            url: "/pages/index/login",
          });
        }
      });
    },
    async onDevice(value){
      let minutes=Number(this.minutes*60).toString(16).padStart(2, '0')
      if(minutes.length>2){ 
        minutes=this.transformNumber(minutes)
      }else{ 
        minutes=minutes+'000000000'
      }
      const params={
        cmd:'data',
        data:value==1?`550c00FF010001${minutes}`:value==2? `550c00FF00000000000000000`:'',
        duration:this.minutes
      }
      if(value==2) delete params['duration']
      const res=await sendMessageApi(params)
      if(res&&res.code==200){ 
        if(value==1){ 
          this.onDeviceName='已启动'
          this.closeDeviceName='关闭设备'
        }else if(value==2){ 
          this.closeDeviceName='已关闭'
          this.onDeviceName='开启设备'
        }
        uni.showToast({
					title: res&&res.msg,
				});
      }else{ 
        uni.showToast({
					title: res&&res.msg,
				});
      }
    },
    async getStatus(){
      const res=await getSwitchStatusApi()
      if(res&&res.isOpen){ 
        this.onDeviceName='已启动'
      }else{ 
        this.onDeviceName='开启设备'
      }
      this.isOpen=res.isOpen||false
      this.minutes=res.duration||0
    },
    transformNumber(num) {
      // 将数字转换成字符串
      let numStr = num.toString();
        // 获取最后两位数字
      let lastTwoDigits = numStr.slice(-2);
      
      // 移除最后两位数字
      let numWithoutLastTwoDigits = numStr.slice(0, -2);
      
      // 将最后两位数字放到字符串开头
      let result = lastTwoDigits + numWithoutLastTwoDigits;
      if(result.length==3){ 
       const numWithoutLastDigit = result.slice(-1);
       const numWithoutLastDigit2 = result.slice(0,-1);
       result=numWithoutLastDigit2+'0'+numWithoutLastDigit
      }
      // 如果不满12位，补0
      while (result.length < 12) {
          result =result+'0';
      }
      return result
    },
  },
};
</script>

<style lang="scss">
page {
  width: 100%;
  height: 100%;
  background-color: #f5f5f9;
}

.head-margin {
  margin-left: 35rpx;
}

.icons-margin {
  margin-right: 35rpx;
}

.con-result-wraper {
  padding: 10% 5%;
}

.head-head {
  color: #8d8d8d;
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
  align-items: center;
}

.head-input {
  width: 100%;
  height: 100rpx;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
}
.onDevice{ 
  line-height: 100rpx;
  text-align: center;
  background-color: #999;
  margin-top: 20rpx;
  font-size: 35rpx;
  font-weight: bold;
}
.closeDevice{
  line-height: 100rpx;
  text-align: center;
  background-color: #999;
  margin-top: 20rpx;
  font-size: 35rpx;
  font-weight: bold;
}
.car_wash_time{ 
  margin-top: 40rpx;
  font-weight: bold;
  color: #333;
  font-size: 26rpx;
}
.minutes{ 
  margin-top: 20rpx;
  background-color: #999;
  line-height: 100rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20rpx;
}
.operate-button {
  width: 100%;
  position: absolute;
  bottom: 10px;
  background-color: #fff;
  border: 1px solid #ff4445;
  height: 76rpx;
  line-height: 76rpx;
  text-align: center;
  color: #ff4445;
  font-size: 30rpx;
  border-radius: 43rpx;
}

.operate-container {
  margin-left: 7%;
  margin-bottom: 3%;
  width: 76%;
  position: absolute;
  bottom: 20px;
  height: 76rpx;
}
</style>
